<?php
/**
 * Created by 王跃达.
 * User: 小马出行
 * Date: 2017/8/24
 * Time: 16:17
 */

namespace app\api\logic;


use my\Define;
use my\Local;
use think\Model;

class CallTaxiLogic extends Model
{


    public function __construct()
    {
        parent::__construct();

    }
    /**
     * 预估价格
     * @param $total 价格  a:3:{s:6:"0-5000";i:8;s:9:"5000-8000";d:1.2;s:10:"8000-8742400";d:1.7;}
     * @param $start_local 出发地
     * @param $arrive_local 到达地
     * @return array
     */
    public function getPrice($total, $start_local, $arrive_local)
    {
        $distance_arr = Local::getDistance($start_local, $arrive_local);
        if ($distance_arr['status'] == 1) {
            $distance = $distance_arr['data'];//实际距离
            return $this->calculatePrice($total,$distance);
        }
        return array("status" => 0);
    }

    /**
     * 根据里程计算价格
     * @param $total
     * @param $distance
     * @return array
     */
    public function calculatePrice($total,$distance){
        $total = json_decode($total,true);
        $mile= $total['mile'];//数组如下
        $start = $total['start'];//起步价数组
//        mile": [
//        {
//            "key": "0-5000",
//            "value": 8
//        },
//        {
//            "key": "5000-8000",
//            "value": 8
//        }]
       // start:{key:3000,value:800}
        $sum = 0;//价格
        if($distance < $start['key']){
            $sum += ($start['value']/100);//转化为元 如果在起步价以内
        }else{
            $sum += ($start['value']/100);//超过起步价距离，先算上起步价
            foreach ($mile as $k => $v) {
                //$k 范围区间5000-8000m  $v单价 1.2元/km
                $dis = explode("-", $v['key']);
                if ($dis[1] < $distance) {
                    $sum += (($dis[1] - $dis[0])/1000 * ($v['value']/100));
                } else {
                    $sum += (($distance - $dis[0]) / 1000 * ($v['value']/100));
                    break;
                }
            }
        }

        $sum = sprintf("%.1f", $sum)*100;
        return array("status" => 1, 'price' => $sum, 'distance' => $distance);
    }
    public function calculatePrice1($total,$distance){
        $total = unserialize($total);
        $sum = 0;//价格
        $i = 0;
        foreach ($total as $k => $v) {
            //$k 范围区间5000-8000m  $v单价 1.2元/km
            $dis = explode("-", $k);
            if ($dis[1] < $distance) {
                if ($i == 0) {
                    $sum = $v;
                } else {
                    $sum += ($dis[1] - $dis[0])/1000 * $v;
                }
            } else {
                if ($i == 0) {
                    $sum = $v;
                } else {
                    $sum += ($distance - $dis[0]) / 1000 * $v;
                }
                break;
            }
            $i++;
        }
        $sum = sprintf("%.1f", $sum)*100;
        return array("status" => 1, 'price' => $sum, 'distance' => $distance);
    }

    /**
     * 获取最大优惠
     * @param $coupon   //优惠券
     * @return array
     */
    public function getMaxPreferential($coupon)
    {
        $preferential_arr=array();
        if(!empty($coupon)){
            foreach($coupon as $v){
                $preferential_arr[]=array("money"=>$v['coupon_price'],"type"=>"coupon","id"=>$v['coupon_id']);
            }
        }
        if(!empty($preferential_arr)){
            usort($preferential_arr, function ($a, $b) {
                $a_pirce = $a['money'];
                $b_pirce = $b['money'];
                if ($a_pirce == $b_pirce)
                    return 0;
                return ($a_pirce > $b_pirce) ? -1 : 1;
            });
            return $preferential_arr[0];
        }
        return array("money"=>0,"type"=>0,"id"=>0);

    }
//    public function getMaxPreferential($coupon, $red, $first, $every)
//    {
//        $preferential_arr=array();
//        if(!empty($coupon)){
//            foreach($coupon as $v){
//                $preferential_arr[]=array("money"=>$v['coupon_price'],"type"=>"coupon","id"=>$v['coupon_id']);
//            }
//        }
//        if(!empty($red)){
//            $red_money=$red['red_money']>Define::MAX_RED?Define::MAX_RED:$red['red_money'];
//            $preferential_arr[]=array("money"=>$red_money,"type"=>"red","id"=>0);
//        }
//        if(!empty($first)){
//            $preferential_arr[]=array("money"=>$first['minus_money'],"type"=>"first","id"=>0);
//        }
//        if(!empty($every)){
//            $preferential_arr[]=array("money"=>$every['minus_money'],"type"=>"every","id"=>0);
//        }
//        if(!empty($preferential_arr)){
//            usort($preferential_arr, function ($a, $b) {
//                $a_pirce = $a['money'];
//                $b_pirce = $b['money'];
//                if ($a_pirce == $b_pirce)
//                    return 0;
//                return ($a_pirce > $b_pirce) ? -1 : 1;
//            });
//            return $preferential_arr[0];
//        }
//        return array("money"=>0,"type"=>0,"id"=>0);
//
//    }




}